import cn.xy.dao.TeacherMapper;
import cn.xy.pojo.Teacher;
import cn.xy.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class test {
    @Test
    public void test01(){
        //获取所有teacher的集合
        SqlSession sqlSeesion = MybatisUtils.getSqlSeesion();
        TeacherMapper mapper = sqlSeesion.getMapper(TeacherMapper.class);
        List<Teacher> teacherList = mapper.getTeacherList();
        for (Teacher teacher : teacherList) {
            System.out.println(teacher);
        }
        sqlSeesion.close();
    }

    @Test
    public void test02(){
        //根据id查询teacher
        SqlSession sqlSession = MybatisUtils.getSqlSeesion();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = mapper.getTeacherById(1);
        System.out.println(teacher);
        sqlSession.close();
    }

    @Test
    public void test03(){
        //增加teacher
        SqlSession sqlSeesion = MybatisUtils.getSqlSeesion();
        TeacherMapper mapper = sqlSeesion.getMapper(TeacherMapper.class);
        Integer integer = mapper.addTeacher(new Teacher(3, "张老师", 1));
        if (integer>0){
            //提交事务
            sqlSeesion.commit();
        }
        //关闭连接
        sqlSeesion.close();
    }

    @Test
    public void test04(){
        //修改teacher信息
        SqlSession sqlSeesion = MybatisUtils.getSqlSeesion();
        TeacherMapper mapper = sqlSeesion.getMapper(TeacherMapper.class);
        Integer integer = mapper.updateTeacher(new Teacher(1, "郝老师", 1));
        if (integer>0){
            sqlSeesion.commit();
        }
        sqlSeesion.close();
    }

    @Test
    public void test05(){
        //根据id删除teacher
        SqlSession sqlSeesion = MybatisUtils.getSqlSeesion();
        TeacherMapper mapper = sqlSeesion.getMapper(TeacherMapper.class);
        Integer integer = mapper.deleteTeacher(3);
        if (integer>0){
            sqlSeesion.commit();
        }
        sqlSeesion.close();
    }
    @Test
    public void test06(){
        //使用万能map参数,进行增加
        SqlSession sqlSeesion = MybatisUtils.getSqlSeesion();
        TeacherMapper mapper = sqlSeesion.getMapper(TeacherMapper.class);
        //创建map集合，放入参数
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("id",3);
        map.put("name","孙老师");
        map.put("cid",2);
        //调用增加的方法
        Integer integer = mapper.addTeacher2(map);
        if (integer>0){
            sqlSeesion.commit();
        }
        sqlSeesion.close();
    }
}
